from flask import Blueprint, request, jsonify
from src.services.llm_service import LLMService
import json

llm_bp = Blueprint('llm', __name__)
llm_service = LLMService()

@llm_bp.route('/industry-analysis', methods=['POST'])
def industry_analysis():
    """行业分析接口"""
    try:
        data = request.get_json()
        prompt = data.get('prompt', '')
        task_id = data.get('task_id', None)
        
        print(f"收到行业分析请求，提示词长度: {len(prompt)}, 任务ID: {task_id}")
        print(f"提示词内容: {prompt[:100]}...")
        
        if not prompt:
            return jsonify({'code': 400, 'message': '缺少提示词'}), 400
        
        # 调用LLM服务，传递task_id用于保存文件
        print("开始调用LLM服务...")
        result = llm_service.analyze_industry(prompt, task_id)
        
        print(f"LLM服务返回结果长度: {len(result)}")
        print(f"返回结果预览: {result[:200]}...")
        
        return jsonify({
            'code': 200,
            'message': '分析成功',
            'data': {
                'content': result
            }
        })
        
    except Exception as e:
        print(f"行业分析接口异常: {str(e)}")
        return jsonify({'code': 500, 'message': f'分析失败: {str(e)}'}), 500
